package com.gxa.medical.controller;

import com.gxa.medical.exception.BusinessException;
import com.gxa.medical.exception.ExceptionCode;
import com.gxa.medical.pojo.User;
import com.gxa.medical.service.UserService;
import com.gxa.medical.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    //用户表插入
    @PostMapping("/register")
    public Map insertUser(@RequestBody User user, @RequestParam("captcha") String captcha, HttpServletRequest request) throws Exception {
        JsonResult jsonResult = new JsonResult();
        HttpSession session = request.getSession();
        String captcha1 = (String) session.getAttribute("captcha");
        //判断用户输入的验证码和存的验证码是否一致
        if (captcha.equals(captcha1)) {
            userService.insertUser(user);
            //把用户的id存在session中
            session.setAttribute("user_id", userService.selectIdByPhone(user.getPhone()).get(0).getId());
            jsonResult.setCode("1");
        } else {
            jsonResult.setCode("0");
            jsonResult.setMsg("验证码错误");
        }
        return jsonResult.getResultMap();
    }

    //用户登录
    @PostMapping("/login")
    public Map loginUser(@RequestBody User user, HttpServletRequest request) throws Exception {
        JsonResult jsonResult = new JsonResult();
        String token = UUID.randomUUID().toString().replaceAll("-", "");
        userService.loginUser(user);
        HttpSession session = request.getSession();
        session.setAttribute("phone", user.getPhone());
        session.setAttribute("token", token);
        jsonResult.setCode("1");
        jsonResult.set("token", token);
        return jsonResult.getResultMap();
    }

    //用户修改密码
    @RequestMapping("/update/pwd")
    public Map updatePwd(User user, HttpServletRequest request) throws Exception {
        JsonResult jsonResult = new JsonResult();
        HttpSession session = request.getSession();
        String phone = (String) session.getAttribute("phone");
        user.setPhone(phone);
        userService.updatePwd(user);
        jsonResult.setCode("1");
        return jsonResult.getResultMap();
    }

    //用户修改消息通知
    @RequestMapping("/update/message")
    public Map updateMessage(@RequestParam("message") String message, HttpServletRequest request) throws Exception {
        JsonResult jsonResult = new JsonResult();
        HttpSession session = request.getSession();
        String phone = (String) session.getAttribute("phone");
        User user = new User();
        if (message.equals("1")) {
            user.setMessage(true);
        } else {
            user.setMessage(false);
        }
        user.setPhone(phone);
        userService.updateMessage(user);
        jsonResult.setCode("1");
        return jsonResult.getResultMap();
    }

    //用户个人信息修改
    @PostMapping("/update/information")
    public Map updateInformation(@RequestBody Map map, HttpServletRequest request) throws Exception {
        JsonResult jsonResult = new JsonResult();
        HttpSession session = request.getSession();
        String phone = (String) session.getAttribute("phone");
        map.put("phone", phone);
        userService.updateInformation(map);
        jsonResult.setCode("1");
        return jsonResult.getResultMap();
    }

}
